package com.probox.common.entity.service.platfom.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import com.probox.common.core.domain.R;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.FreightTypeEnum;
import com.probox.common.core.enums.FrequencyEnum;
import com.probox.common.core.exception.PlatformException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.utils.money.MoneyUtils;
import com.probox.common.entity.domain.platfom.vo.PlatformFreightMoudleVo;
import com.probox.platform.api.domain.PlatformFreightVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.platfom.PlatformFreightMapper;
import com.probox.common.entity.domain.platfom.PlatformFreight;
import com.probox.common.entity.service.platfom.IPlatformFreightService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 平台邮费设置Service业务层处理
 *
 * @author usoft
 * @date 2021-06-28
 */
@Service
@Transactional
public class PlatformFreightServiceImpl extends ServiceImpl<PlatformFreightMapper, PlatformFreight> implements IPlatformFreightService {
    @Autowired
    private PlatformFreightMapper platformFreightMapper;


    /**
     * 查询平台邮费设置
     *
     * @param freightUid 平台邮费设置ID
     * @return 平台邮费设置
     */
    @Override
    public PlatformFreight selectPlatformFreightById(Long freightUid) {
        return this.getById(freightUid);
    }

    /**
     * 查询平台邮费设置列表
     *
     * @param platformFreight 平台邮费设置
     * @return 平台邮费设置
     */
    @Override
    public List<PlatformFreight> queryList(PlatformFreight platformFreight) {
        QueryWrapper<PlatformFreight> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightUid())) && platformFreight.getFreightUid() != null) {
            queryWrapper.eq("FREIGHT_UID", platformFreight.getFreightUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getMoudleUid())) && platformFreight.getMoudleUid() != null) {
            queryWrapper.eq("MOUDLE_UID", platformFreight.getMoudleUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightProvince())) && platformFreight.getFreightProvince() != null) {
            queryWrapper.eq("FREIGHT_PROVINCE", platformFreight.getFreightProvince());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightType())) && platformFreight.getFreightType() != null) {
            queryWrapper.eq("FREIGHT_TYPE", platformFreight.getFreightType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightFate())) && platformFreight.getFreightFate() != null) {
            queryWrapper.eq("FREIGHT_FATE", platformFreight.getFreightFate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightInitFate())) && platformFreight.getFreightInitFate() != null) {
            queryWrapper.eq("FREIGHT_INIT_FATE", platformFreight.getFreightInitFate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getPlatformUid())) && platformFreight.getPlatformUid() != null) {
            queryWrapper.eq("PLATFORM_UID", platformFreight.getPlatformUid());
        }
        if (StringUtils.isNotEmpty(platformFreight.getDataStatus())){
            queryWrapper.eq("DATA_STATUS", platformFreight.getDataStatus());
        }
        return platformFreightMapper.selectList(queryWrapper);
    }

    public List<PlatformFreight> queryValidateList(PlatformFreight platformFreight) {
        QueryWrapper<PlatformFreight> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightUid())) && platformFreight.getFreightUid() != null) {
            queryWrapper.eq("FREIGHT_UID", platformFreight.getFreightUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getMoudleUid())) && platformFreight.getMoudleUid() != null) {
            queryWrapper.eq("MOUDLE_UID", platformFreight.getMoudleUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightProvince())) && platformFreight.getFreightProvince() != null) {
            queryWrapper.eq("FREIGHT_PROVINCE", platformFreight.getFreightProvince());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightType())) && platformFreight.getFreightType() != null) {
            queryWrapper.eq("FREIGHT_TYPE", platformFreight.getFreightType());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightFate())) && platformFreight.getFreightFate() != null) {
            queryWrapper.eq("FREIGHT_FATE", platformFreight.getFreightFate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getFreightInitFate())) && platformFreight.getFreightInitFate() != null) {
            queryWrapper.eq("FREIGHT_INIT_FATE", platformFreight.getFreightInitFate());
        }
        if (StringUtils.isNotEmpty(String.valueOf(platformFreight.getPlatformUid())) && platformFreight.getPlatformUid() != null) {
            queryWrapper.eq("PLATFORM_UID", platformFreight.getPlatformUid());
        }
        if (StringUtils.isNotEmpty(platformFreight.getDataStatus())){
            queryWrapper.eq("DATA_STATUS", platformFreight.getDataStatus());
        }
        queryWrapper.last(" and MOUDLE_UID in (SELECT moudle_uid from platform_freight_moudle where data_status='1') ");
        return platformFreightMapper.selectList(queryWrapper);
    }

    /**
     * 删除邮费信息
     * @param moudleUid
     */
    @Override
    public void removeFreight(Long [] moudleUid){
        UpdateWrapper<PlatformFreight> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("MOUDLE_UID", moudleUid);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());

        this.update(updateWrapper);
    }

    /**
     * 新增平台邮费设置
     *
     * @param vo 平台邮费设置
     * @return 结果
     */
    @Override
    public void insertPlatformFreight(PlatformFreightMoudleVo vo) {
        Long [] uid ={vo.getMoudleUid()};
        removeFreight(uid);
        String[] freeShippingList = vo.getFreeShippingList();// 包邮
        if (ObjectUtil.isNotEmpty(freeShippingList)) {
            for (int i = 0; i < freeShippingList.length; i++) {
                save(vo, freeShippingList[i], FreightTypeEnum.freeShipping.getKey());
            }
        }
        String[] aloeGelList = vo.getAloeGelList();// 不包邮
        if (ObjectUtil.isNotEmpty(aloeGelList)) {
            for (int i = 0; i < aloeGelList.length; i++) {
                save(vo, aloeGelList[i], FreightTypeEnum.aloeGel.getKey());
            }
        }
        String[] noDeliveryList = vo.getNoDeliveryList();// 不配送
        if (ObjectUtil.isNotEmpty(noDeliveryList)) {
            for (int i = 0; i < noDeliveryList.length; i++) {
                save(vo, noDeliveryList[i], FreightTypeEnum.noDelivery.getKey());
            }
        }


    }

    /**
     * 修改平台邮费设置
     *
     * @param platformFreight 平台邮费设置
     * @return 结果
     */
    @Override
    public boolean updatePlatformFreight(PlatformFreight platformFreight) {

        return this.updateById(platformFreight);
    }

    /**
     * 批量删除平台邮费设置
     *
     * @param freightUids 需要删除的平台邮费设置ID
     * @return 结果
     */
    @Override
    public boolean deletePlatformFreightByIds(Long[] freightUids) {
        return this.removeByIds(Arrays.asList(freightUids));
    }

    private void save(PlatformFreightMoudleVo vo, String name, String type) {
        PlatformFreight freight = null;
        if (FreightTypeEnum.freeShipping.getKey().equals(type)) {
            freight = new PlatformFreight(vo.getMoudleUid(), MoneyUtils.changeY2B(vo.getMoudleAmount()), name, type,
                    MoneyUtils.changeY2B(vo.getMoudleAmount()), MoneyUtils.changeY2B(vo.getFreightInitFate()), vo.getPlatformUid(), DataStatus.valid.getKey());
        }
        if (FreightTypeEnum.aloeGel.getKey().equals(type)) {
            freight = new PlatformFreight(vo.getMoudleUid(), MoneyUtils.changeY2B(vo.getMoudleAmount()), name, type,
                    BigDecimal.ZERO, MoneyUtils.changeY2B(vo.getPlatfreightInitFate()), vo.getPlatformUid(), DataStatus.valid.getKey());
        }
        if (FreightTypeEnum. noDelivery.getKey().equals(type)) {
            freight = new PlatformFreight(vo.getMoudleUid(), MoneyUtils.changeY2B(vo.getMoudleAmount()), name, type,
                    BigDecimal.ZERO, BigDecimal.ZERO, vo.getPlatformUid(), DataStatus.valid.getKey());
        }
        boolean save = this.save(freight);
        if (!save) throw new PlatformException("添加失败");
    }

    @Override
    public PlatformFreightMoudleVo getVo(PlatformFreightMoudleVo vo) {
        HashMap map = new HashMap();
        map.put("moudleUid", vo.getMoudleUid());
        map.put("freightType", FreightTypeEnum.freeShipping.getKey());
        vo.setFreeShippingList(platformFreightMapper.getTypeArr(map));
        map.put("freightType", FreightTypeEnum.noDelivery.getKey());
        vo.setNoDeliveryList(platformFreightMapper.getTypeArr(map));
        QueryWrapper<PlatformFreight> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        queryWrapper.eq("FREIGHT_TYPE", FreightTypeEnum.aloeGel.getKey());
        queryWrapper.eq("MOUDLE_UID", vo.getMoudleUid());
        List<PlatformFreight> platformFreights = platformFreightMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(platformFreights)) return vo;
        String[] arr = new String[platformFreights.size()];
        for (int i = 0; i < platformFreights.size(); i++) {
            PlatformFreight platformFreight = platformFreights.get(i);
            arr[i] = platformFreight.getFreightProvince();
            if (i == 0) {
                vo.setPlatfreightInitFate(MoneyUtils.changeF2B2(platformFreight.getFreightInitFate()));
            }
        }
        vo.setAloeGelList(arr);
        return vo;
    }

    /**
     * 获取邮费 api
     * @param freightVo
     * @return
     */
    @Override
    public PlatformFreightVo getFreight(PlatformFreightVo freightVo){
        PlatformFreight freight = new PlatformFreight();
        freight.setFreightProvince(freightVo.getFreightProvince());
        freight.setPlatformUid(freightVo.getPlatformUid());
        freight.setDataStatus(DataStatus.valid.getKey());
        List<PlatformFreight> list = queryValidateList(freight);
        if (list.size()>0){
            PlatformFreightVo vo = new PlatformFreightVo();
            BeanUtils.copyProperties(list.get(0),vo);
            return vo;
        }
        return null;
    }

}
